package com.compomics.icelogo.gui.forms;

import com.compomics.icelogo.core.aaindex.AAIndexMatrix;
import com.compomics.icelogo.core.aaindex.AAIndexParameterMatrix;
import com.compomics.icelogo.core.data.MainInformationFeeder;
import com.compomics.icelogo.core.enumeration.AminoAcidEnum;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: niklaas
 * Date: 26-feb-2009
 * Time: 16:00:19
 * To change this template use File | Settings | File Templates.
 */
public class AAIndexChooserForm {
    private JTextField txtSearch;
    private JList listAaMatrix;
    private JPanel jpanContent;
    private JTextField txtAuthor;
    private JTextField txtPublication;
    private JTextField txtJournal;
    private JTextField txtPubmed;
    private JTextArea txtMatrix;
    private JCheckBox useSlidingWindowCheckBox;
    private JSpinner spinner1;

    private Vector<AAIndexParameterMatrix> iSelectedMatrix = new Vector<AAIndexParameterMatrix>();
    private Vector<AAIndexMatrix> iAllMatrix = new Vector<AAIndexMatrix>();
    private MainInformationFeeder iInfoFeeder;


    public AAIndexChooserForm() {
        iInfoFeeder = MainInformationFeeder.getInstance();
        $$$setupUI$$$();
        useSlidingWindowCheckBox.setSelected(iInfoFeeder.isSlidingWindowInAaParameter());

        spinner1.addChangeListener(new ChangeListener() {
            public void stateChanged(final ChangeEvent e) {
                MainInformationFeeder.getInstance().setSlidingWindowSize((Integer) spinner1.getValue());
            }
        });
        useSlidingWindowCheckBox.addChangeListener(new ChangeListener() {
            public void stateChanged(final ChangeEvent e) {
                MainInformationFeeder.getInstance().setSlidingWindowInAaParameter(useSlidingWindowCheckBox.isSelected());
            }
        });


        iAllMatrix = iInfoFeeder.getAaParameterMatrixes();

        txtSearch.addKeyListener(new KeyListener() {
            public void keyTyped(KeyEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void keyPressed(KeyEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void keyReleased(KeyEvent e) {

                searchAaMatrix();
            }
        });

    }

    public JPanel getContentPane() {
        return jpanContent;
    }

    private void createUIComponents() {
        listAaMatrix = new JList(iSelectedMatrix);
        ListSelectionModel listSelectionModel = listAaMatrix.getSelectionModel();
        listSelectionModel.addListSelectionListener(new listSelectionHandler());
        spinner1 = new JSpinner();
        spinner1.setModel(new SpinnerNumberModel(iInfoFeeder.getSlidingWindowSize(), 2, 100, 1));
    }

    public void searchAaMatrix() {
        String lSearch = txtSearch.getText();
        if (lSearch.length() > 0) {
            iSelectedMatrix.removeAllElements();

            for (int i = 0; i < iAllMatrix.size(); i++) {
                if (iAllMatrix.get(i).getTitle().toLowerCase().indexOf(lSearch.toLowerCase()) > -1) {
                    iSelectedMatrix.add((AAIndexParameterMatrix) iAllMatrix.get(i));
                }
            }
            listAaMatrix.updateUI();
        }
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        jpanContent = new JPanel();
        jpanContent.setLayout(new GridBagLayout());
        txtSearch = new JTextField();
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(txtSearch, gbc);
        final JLabel label1 = new JLabel();
        label1.setText("Search Aa parameter");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(label1, gbc);
        final JScrollPane scrollPane1 = new JScrollPane();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 5;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(scrollPane1, gbc);
        scrollPane1.setViewportView(listAaMatrix);
        final JLabel label2 = new JLabel();
        label2.setText("Authors: ");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(label2, gbc);
        final JLabel label3 = new JLabel();
        label3.setText("Publication title: ");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(label3, gbc);
        final JLabel label4 = new JLabel();
        label4.setText("Journal: ");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(label4, gbc);
        final JLabel label5 = new JLabel();
        label5.setText("Pubmedid: ");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(label5, gbc);
        final JLabel label6 = new JLabel();
        label6.setText("Matrix: ");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(label6, gbc);
        final JScrollPane scrollPane2 = new JScrollPane();
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 7;
        gbc.gridwidth = 2;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(scrollPane2, gbc);
        txtMatrix = new JTextArea();
        txtMatrix.setEditable(false);
        txtMatrix.setRows(2);
        scrollPane2.setViewportView(txtMatrix);
        txtPubmed = new JTextField();
        txtPubmed.setEditable(false);
        txtPubmed.setHorizontalAlignment(4);
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 6;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(txtPubmed, gbc);
        useSlidingWindowCheckBox = new JCheckBox();
        useSlidingWindowCheckBox.setText("Use sliding window");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(useSlidingWindowCheckBox, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 4;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(spinner1, gbc);
        final JLabel label7 = new JLabel();
        label7.setText("window size: ");
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(label7, gbc);
        txtAuthor = new JTextField();
        txtAuthor.setEditable(false);
        txtAuthor.setHorizontalAlignment(4);
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(txtAuthor, gbc);
        txtPublication = new JTextField();
        txtPublication.setEditable(false);
        txtPublication.setHorizontalAlignment(4);
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(txtPublication, gbc);
        txtJournal = new JTextField();
        txtJournal.setEditable(false);
        txtJournal.setHorizontalAlignment(4);
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 5;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(txtJournal, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return jpanContent;
    }

    private class listSelectionHandler implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {
            AAIndexParameterMatrix lMatrix = (AAIndexParameterMatrix) listAaMatrix.getSelectedValue();
            txtAuthor.removeAll();
            txtAuthor.setText(lMatrix.getAuthors());
            txtJournal.removeAll();
            txtJournal.setText(lMatrix.getJournal());
            txtPublication.removeAll();
            txtPublication.setText(lMatrix.getDescription());
            txtPubmed.removeAll();
            txtPubmed.setText(lMatrix.getPubMedId());
            txtMatrix.removeAll();
            String lLine1 = "";
            String lLine2 = "";
            for (AminoAcidEnum aa : AminoAcidEnum.values()) {
                lLine1 = lLine1 + aa.getOneLetterCode() + "\t";
                lLine2 = lLine2 + Math.round((Double) lMatrix.getValueForAminoAcid(aa) * 100.0) / 100.0 + "\t";
            }
            txtMatrix.setText(lLine1 + "\n" + lLine2);

            iInfoFeeder.setSelectedAaParameterMatrix(lMatrix);
        }
    }
}
